package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.jianyun.jyzs.MainActivity;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.UrlConfig;
import com.jianyun.jyzs.dao.MessageRemind;
import com.jianyun.jyzs.dao.ModelDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.presenter.SplashPersenter;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.view.iview.ISplashView;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 启动界面
 * Created by yangshijun on 2018/5/23.
 */

public class SplashActivity extends MvpActivity<ISplashView, SplashPersenter> implements ISplashView {


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            getToActiviy();

        }
    };
    @BindView(R.id.ll_gen)
    LinearLayout llGen;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rce_activity_splash);
        ButterKnife.bind(this);
        try {
            String startPageUrl = LoginCache.getInstance().getLoginCache().getStartPageUrl();
            if (startPageUrl != null && !TextUtils.isEmpty(startPageUrl)) {
                SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        llGen.setBackground(resource);
                    }
                };
                Glide.with(this).load(startPageUrl).into(simpleTarget);
                Message message = new Message();
                message.arg1 = 1;
                handler.sendMessageDelayed(message, 3000);
            } else {
                getToActiviy();
            }
        } catch (Exception e) {
            e.printStackTrace();
            getToActiviy();
        }
        initGetYunCenter();

    }

    private void initGetYunCenter() {
        try {
            final String enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
            if (enterpriseCode != null) {
                Api api = RetrofitHelper.builder().create(Api.class);
                api.getConfig("GetConfig", enterpriseCode, "jyzs", "Android", "1.0.2")
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new CustomCallback<UrlConfig>() {
                            @Override
                            protected void onResponse(UrlConfig response) {
                                if (response.isResult()) {
                                    Log.i("test", "云中心：" + response.toString());
                                    response.setEnterpriseCode(enterpriseCode);
                                    //保存云中心URL
                                    LoginCache.getInstance().setLoginCache(response);
                                } else {

                                }
                            }

                            @Override
                            protected void onException(String msg) {
                                //onJyLoginListener.loginResult(false, msg);
                            }


                        });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private void getToActiviy() {
        boolean validCache = false;
        try {
            validCache = LoginCache.getInstance().isCacheValid();
        } catch (Exception e) {
            e.printStackTrace();
        }


        if (validCache) {
            MessageRemind remind = new MessageRemind(this);
            ModelDao modelDao = new ModelDao(this);
            //缓存有效，优先进入 main 界面
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @NonNull
    @Override
    public SplashPersenter createPresenter() {
        return new SplashPersenter();
    }

    @Override
    public void setBtimap(Bitmap btimap) {

    }

    @Override
    public void onFailed() {

    }

    @Override
    public void setImgPath(String imgPath) {

    }
}
